﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace code
{
    public partial class Form1 : Form
    {
        string beforeExpression = "";//expreesion比beforeExpreesion多的部分就是最近输入的数字，将它压入栈中
        string opetator1 = "";//为了比较运算符优先级，暂存运算符，此为前一个
        string opetator2 = "";//为了比较运算符优先级，暂存运算符，此为中间一个
        string opetator3 = "";//为了比较运算符优先级，暂存运算符，此为后一个
        int flag = 1;//记录当前是否为空值(是==1；否==0)；
        int flag2 = 0;//记录前一个输入是数字还是符号（不能连续输入两个符号）,数字==0；符号==1；
        Stack<double> stack = new Stack<double>();//依顺序存放输入的数字
        public Form1()
        {
            InitializeComponent();
        }
        void addNum(int n)//用户输入为数字
        {
            textBox1.Text += n;
            flag= 0;
            flag2= 0;
        }
        void addOperator(string str)//用户输入为符号
        {
            if (opetator1 == "")
            {
                opetator1 = str;
            }
            else if (opetator2 == "")
            {
                opetator2 = str;
            }
            else
            {
                opetator3 = str;
            }
            if (flag == 1)//如果当前为空值，直接输入符号为违法
            {
                MessageBox.Show("请先输入数字");
                return;
            }
            if (flag2 == 1)//若前一个输入为符号，则再次输入符号为替换符号
            {
                textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length-1, 1);
                textBox1.Text += str;
                return;
            }
            stack.Push(double.Parse(textBox1.Text.Substring(beforeExpression.Length)));
            textBox1.Text += str;
            beforeExpression= textBox1.Text;
            calculate();
        }
        void calculate()
        {
            if (stack.Count > 2)
            {
                if (opetator3 == "=")
                {
                    if (opetator1 == "*" || opetator1 == "/")
                    {
                        double num3 = stack.Pop();
                        double num2 = stack.Pop();
                        double num1 = stack.Pop();
                        yunSuan(num2, opetator1, num1);
                        yunSuan(stack.Pop(), opetator2, num3);
                        over();
                        opetator1 = opetator3;
                        opetator2 = "";
                        opetator3 = "";
                    }
                    else
                    {
                        yunSuan(stack.Pop(), opetator2, stack.Pop());
                        yunSuan(stack.Pop(), opetator1, stack.Pop());
                        over();
                        opetator1 = opetator3;
                        opetator2 = "";
                        opetator3 = "";                
                    }
                }
                else
                { 
                    if (opetator1 == "-" || opetator1 == "+")
                    {
                        if (opetator2 == "-" || opetator2 == "+")
                        {
                            double num3 = stack.Pop();
                            double num2 = stack.Pop();
                            double num1 = stack.Pop();
                            yunSuan(num2, opetator1, num1);
                            stack.Push(num3);
                            opetator1 = opetator2;
                            opetator2 = opetator3;
                            opetator3 = "";
                        }
                        else
                        {
                            yunSuan(stack.Pop(), opetator2, stack.Pop());
                            opetator2 = opetator3;
                            opetator3 = "";
                        }
                    }
                    else
                    {
                        double num3 = stack.Pop();
                        double num2 = stack.Pop();
                        double num1 = stack.Pop();
                        yunSuan(num2, opetator1, num1);
                        stack.Push(num3);
                        opetator1 = opetator2;
                        opetator2 = opetator3;
                        opetator3 = "";
                    }
                }
                return;
            }
            if (stack.Count > 1)
            {
                if (opetator2 == "=")
                {
                    yunSuan(stack.Pop(), opetator1, stack.Pop());
                    over();
                    opetator1 = "";
                    opetator2 = "";
                    opetator3 = "";
                    beforeExpression = "";
                }
                return;
            }
            if (opetator1 == "=")
            {
                over();
                opetator1 = "";
                opetator2 = "";
                opetator3 = "";
                beforeExpression = "";
                return;
            }
        }
        void yunSuan(double num2,string str,double num1)
        {
            if (str == "+")
            {
                stack.Push(num1 + num2);
            }
            else if (str == "-")
            {
                stack.Push(num1 - num2);
            }
            else if (str == "*")
            {
                stack.Push(num1 * num2);
            }
            else
            {
                stack.Push(num1 / num2);
            }
        }
        void over()
        {
            textBox1.Text += Environment.NewLine; 
            textBox1.Text += stack.Peek().ToString();
        }
        private void button10_Click(object sender, EventArgs e)//数字按钮0
        {
            addNum(0);
        }
        private void button7_Click(object sender, EventArgs e)//数字按钮1
        {
            addNum(1);
        }
        private void button8_Click(object sender, EventArgs e)//数字按钮2
        {
            addNum(2);
        }
        private void button9_Click(object sender, EventArgs e)//数字按钮3
        {
            addNum(3);
        }
        private void button4_Click(object sender, EventArgs e)//数字按钮4
        {
            addNum(4);
        }
        private void button5_Click(object sender, EventArgs e)//数字按钮5
        {
            addNum(5);
        }
        private void button6_Click(object sender, EventArgs e)//数字按钮6
        {
            addNum(6);
        }
        private void button1_Click(object sender, EventArgs e)//数字按钮7
        {
            addNum(7);
        }
        private void button2_Click(object sender, EventArgs e)//数字按钮8
        {
            addNum(8);
        }
        private void button3_Click(object sender, EventArgs e)//数字按钮9
        {
            addNum(9);
        }
        private void button14_Click(object sender, EventArgs e)//符号按钮*
        {
            addOperator("*");
        }
        private void button15_Click(object sender, EventArgs e)//符号按钮/
        {
            addOperator("/");
        }
        private void button11_Click(object sender, EventArgs e)//符号按钮+
        {
            addOperator("+");
        }
        private void button12_Click(object sender, EventArgs e)//符号按钮-
        {
            addOperator("-");
        }
        private void button16_Click(object sender, EventArgs e)//符号按钮=
        {
            addOperator("=");
        }
        private void button13_Click(object sender, EventArgs e)//清除按钮
        {
            textBox1.Text = "";
            beforeExpression = "";
            opetator1 = "";
            opetator2 = "";
            opetator3 = "";
            flag = 1;
            flag2 = 0;
            stack.Clear();
        }
    }
}
